package com.example.controller;

import com.example.bean.crm.wlb;
import com.example.bean.crm.zyb;
import com.example.service.crmService.crmService;
import com.example.service.k3Service.k3Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * 物料模块
 * Created by tony on 2017/12/15.
 */
@Component
@RestController
public class wlControl {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //定义k3Service
    @Autowired
    private k3Service k3Service;
    //定义mesService
    @Autowired
    private crmService crmService;

    /**
     * 物料初始化
     */
    @RequestMapping("/initializationWl")
    public void initializationWl() {
        logger.info("物料初始化。开始……");
        try {
            String model = "ALL";
            //取得全部物料信息
            ArrayList<wlb> wlList = k3Service.selectAllWl(model);
            //清空物料中间表
            crmService.deleteAllWl();
//            for (wlb wlb : wlList) {
//                //品级
//                String pl = wlb.getPl();
//                String[] sourceStrArray = pl.split("_");
//                for (int i = 0; i < sourceStrArray.length; i++) {
//                    switch (i) {
//                        case 0:
//                            wlb.setPl1j(sourceStrArray[i]);
//                            break;
//                        case 1:
//                            wlb.setPl2j(sourceStrArray[i]);
//                            break;
//                        case 2:
//                            wlb.setPl3j(sourceStrArray[i]);
//                            break;
//                        case 3:
//                            wlb.setPl4j(sourceStrArray[i]);
//                            break;
//                        default:
//                            break;
//                    }
//
//                }
//
//            }
            //中间表中插入全部物料信息
            crmService.initializationWl(wlList);
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        logger.info("物料初始化。结束。");
    }

    /**
     * 物料更新
     */
    @RequestMapping("/insertWl")
    @Scheduled(cron = "0 0 1 * * ?") //物料更新每天凌晨1点执行一次
    public void insertZyScheduled() {
        logger.info("物料更新每天凌晨1点执行一次。开始……");

        String model = "NEW";
        //取得更新的物料信息
        ArrayList<wlb> wlList = k3Service.selectAllWl(model);
        //判断更新和新建物料
        for (wlb wlb : wlList) {
            try {
                //品级
//                String pl = wlb.getPl();
//                String[] sourceStrArray = pl.split("_");
//                for (int i = 0; i < sourceStrArray.length; i++) {
//                    switch (i) {
//                        case 0:
//                            wlb.setPl1j(sourceStrArray[i]);
//                            break;
//                        case 1:
//                            wlb.setPl2j(sourceStrArray[i]);
//                            break;
//                        case 2:
//                            wlb.setPl3j(sourceStrArray[i]);
//                            break;
//                        case 3:
//                            wlb.setPl4j(sourceStrArray[i]);
//                            break;
//                        default:
//                            break;
//                    }
//
//                }
                int wlbFlg = wlb.getCRMclyf();
                //更新
                if (wlbFlg == 1) {
                    //更新
                    crmService.updateWl(wlb);
                    logger.info("物料更新" + wlb.getDaima() + "结束。");
                } else if (wlbFlg == 0) {
                    //新增
                    crmService.insertWl(wlb);
                    k3Service.updateWl(wlb.getERPid());
                    logger.info("物料新增" + wlb.getDaima() + "结束。");
                }
            } catch (Exception e) {
                logger.info(e.getMessage());
            }
        }
        logger.info("物料更新每天凌晨1点执行一次。结束。");

    }

}
